
#include <gtk/gtk.h>
#include "loginDialog.h"

typedef struct
{
    GtkWidget *user;
    GtkWidget *passwd;
}ARG;
void show_passwd(GtkWidget *widget,ARG *arg)
{
    g_print("UserName:%s\nPasswd:%s\n",gtk_entry_get_text(GTK_ENTRY(arg->user)),gtk_entry_get_text(GTK_ENTRY(arg->passwd)));
    gtk_main_quit();
}
int login_main()
{
    GtkWidget *dialog;
    GtkWidget *vbox;
    GtkWidget *hbox;
    GtkWidget *label;
    GtkWidget *button;
    GtkWidget *sep;
    GtkWidget *user;
    GtkWidget *passwd;
    ARG arg;
    //gtk_init(&argc,&argv);
    dialog=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(dialog),"Login");
    gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(dialog),20);
    g_signal_connect(G_OBJECT(dialog),"destroy",G_CALLBACK(gtk_main_quit),NULL);
    
    vbox=gtk_vbox_new(FALSE,0);
    gtk_container_add(GTK_CONTAINER(dialog),vbox);
    hbox=gtk_hbox_new(FALSE,0);
    gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);
    label=gtk_label_new("UserName:");
    gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
    user=gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(hbox),user,FALSE,FALSE,0);
    hbox=gtk_hbox_new(FALSE,0);
    gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);
    label=gtk_label_new("Passwd: ");
    gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5);
    passwd=gtk_entry_new();
    gtk_entry_set_visibility(GTK_ENTRY(passwd),FALSE);         //设置输入密码时不会显示输入的字符
    gtk_box_pack_start(GTK_BOX(hbox),passwd,FALSE,FALSE,5);
    sep=gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(vbox),sep,FALSE,FALSE,5);
    button=gtk_button_new_with_label("OK");
    gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);
    arg.user=user;
    arg.passwd=passwd;
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(show_passwd),&arg);
    gtk_widget_show_all(dialog);
    
    gtk_main();
    return 0;
}

    
//无模式窗口  
//#define SCHEMALESS_WINDOW  
int login_callback(GtkWindow *parent,gchar *username,gchar *password)  
{  
    GtkWidget *dialog;  
    GtkWidget *label;  
    GtkWidget *label1;  
    GtkWidget *label2; 
    GtkWidget *user;  
    GtkWidget *hbox;  
    GtkWidget *hbox1; 
    GtkWidget *hbox2; 
    GtkWidget *passwd; 
    
    
    gint result;  
    /*GtkWidget * gtk_dialog_new_with_buttons( 
     *           const gchar *title,对话框标题 
     *           GtkWindow *parent,对话框父窗口 
     *           GtkDialogFlags flags, 
     *           const gchar *first_button_text,动作区域按钮列表 
     *           ...); 
     *           falgs:GTK_DIALOG_MODAL:对话框始终保持在父窗口的上部，直到关闭，防止父窗口交互 
     *                 GTK_DIALOG_DESTROY_WITH_PARENT:父窗口销毁，对话框也销毁 
     *                 GTK_DIALOG_NO_SEPARATOR:不显示分割线 
     * 
     * */  
#ifndef SCHEMALESS_WINDOW  
    dialog = gtk_dialog_new_with_buttons("login",parent,GTK_DIALOG_MODAL,GTK_STOCK_OK,GTK_RESPONSE_OK,NULL);  
#else  
    dialog = gtk_dialog_new_with_buttons("login",parent,GTK_DIALOG_DESTROY_WITH_PARENT,GTK_STOCK_OK,GTK_RESPONSE_OK,NULL);  
#endif  
    //设置缺省按钮  
    gtk_dialog_set_default_response(GTK_DIALOG(dialog),GTK_RESPONSE_OK); 
    
    

    hbox=gtk_hbox_new(FALSE,0);   
    hbox1=gtk_hbox_new(FALSE,0);    
     hbox2=gtk_hbox_new(FALSE,0);    
    label=gtk_label_new("语音技术由科大讯飞提供\nhttp://www.xfyun.cn/");    
    label1=gtk_label_new("UserName:");        
    label2=gtk_label_new("Passwd: ");  
    user=gtk_entry_new();
    passwd=gtk_entry_new();
    gtk_entry_set_visibility(GTK_ENTRY(passwd),FALSE);         //设置输入密码时不会显示输入的字符
    //4.设置行编辑显示最大字符的长度  
    gtk_entry_set_max_length(GTK_ENTRY(user), 16);
    gtk_entry_set_max_length(GTK_ENTRY(passwd), 16);
     
    GtkWidget* vbox1= gtk_vbox_new(FALSE,5);
    gtk_container_set_border_width(GTK_CONTAINER(hbox),10);  
    gtk_container_set_border_width(GTK_CONTAINER(hbox1),10);
    gtk_container_set_border_width(GTK_CONTAINER(hbox2),10);
    gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,5); 
    gtk_box_pack_start(GTK_BOX(vbox1),hbox,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(hbox1),label1,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(hbox1),user,FALSE,FALSE,5); 
    gtk_box_pack_start(GTK_BOX(vbox1),hbox1,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(hbox2),label2,FALSE,FALSE,5);  
    gtk_box_pack_start(GTK_BOX(hbox2),passwd,FALSE,FALSE,5); 
    gtk_box_pack_start(GTK_BOX(vbox1),hbox2,FALSE,FALSE,5);    
   gtk_container_add ( GTK_DIALOG ( dialog ), vbox1 );
    gtk_widget_show_all(dialog);  
  
#ifndef SCHEMALESS_WINDOW      
    //运行和输出对话框  
    result = gtk_dialog_run(GTK_DIALOG(dialog));  
  
    switch(result){  
        case GTK_RESPONSE_NONE:  
            /*-1 
             * 对话框被窗口管理器销毁或通过gtk_widget_destroy()有其他程序销毁。 
             * 如果构件没有设置一个相应标识，会返回它。 
             */  
            break;  
        case GTK_RESPONSE_REJECT:  
            /* -2 
             * 这个标识没有和对话框内置的任何按钮相关联，可以任意使用它 
             * */  
            break;  
        case GTK_RESPONSE_ACCEPT:  
            /* -3 
             * 这个标识没有和对话框内置的任何按钮相关联，可以任意使用它 
             * */  
            break;  
        case GTK_RESPONSE_DELETE_EVENT:  
            /* -4 
             * 每个对话框都会自动连接到delete-event信号，当gtk_dialog_run() 
             * 运行是，这个标识会被返回，而且delete-event会被终止、不会像 
             * 往常那样销毁窗口 
             * */  
            break;  
              
        case GTK_RESPONSE_OK:  
            /* -5 
             * GTK_STOCK_OK 被点击 
             */  
            sprintf(username,"%s",gtk_entry_get_text(GTK_ENTRY(user)));
            sprintf(password,"%s",gtk_entry_get_text(GTK_ENTRY(passwd)));
            //printf("%s\n",instr);
             break;  
        case GTK_RESPONSE_CANCEL:  
            /*-6 
             * GTK_STOCK_CANCEL被点击 
             * */  
            g_print("cancel is press!\n");  
  
            break;  
        case GTK_RESPONSE_CLOSE:  
            /* -7 
             *GTK_STOCK_CLOSE被点击 
             */  
            break;  
        case GTK_RESPONSE_YES:  
            /* -8 
             *GTK_STOCK_YES被点击 
             */  
            break;  
        case GTK_RESPONSE_NO:  
            /* -9 
             *GTK_STOCK_NO被点击 
             */  
            break;  
        case GTK_RESPONSE_APPLY:  
            /* -10 
             *GTK_STOCK_APPLY被点击 
             */  
            break;  
        case GTK_RESPONSE_HELP:  
            /* -11 
             *GTK_STOCK_HELP被点击 
             */  
            break;  
        default:  
            g_print("something wrong!\n");  
  
            break;  
    }  
    gtk_widget_destroy(dialog);  
    //printf("%s\n",instr);
#else  
    sprintf(instr,"%s",gtk_entry_get_text(GTK_ENTRY(entry)));
    g_signal_connect(G_OBJECT(dialog),"response",G_CALLBACK(gtk_widget_destroy),NULL);  
#endif  
  return result;
}  
  
